package com.gidgabs.modules.api.controller;

import com.gidgabs.common.annotation.AuthIgnoreApi;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbOrderDataDto;
import com.gidgabs.dto.TbOrderDto;
import com.gidgabs.modules.api.service.TbOrderService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbOrderEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 产品订单表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-02 10:07:40
 */
@Api("订单模块")
@RestController
@RequestMapping("/api/order")
public class TbOrderController {

    @Autowired
    private TbOrderService orderService;

    @Autowired
    private TbUserService userService;

    /**
     * 查询订单列表 根据订单状态
     *
     * @param orderStatus
     * @return
     */
    @AuthIgnoreApi
    @PostMapping("/getOrderList")
    @ApiOperation(value = "查询订单列表 根据订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderStatus", value = "订单状态 1待支付 2 待接收 3 待评价 4 已退款", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<TbOrderDto> getOrderList(Integer orderStatus) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        TbOrderEntity tbOrderEntity = new TbOrderEntity();
        tbOrderEntity.setUserId(user.getUserId());
        tbOrderEntity.setOrderStatus(orderStatus);
        List<TbOrderEntity> orderEntity = orderService.selectOrderListByObj(tbOrderEntity);
        List<TbOrderDto> tbOrderDtoList = new ArrayList<>();
        for (int i = 0; i < orderEntity.size(); i++) {
            tbOrderDtoList.add(orderEntity.get(i).toDto());
        }
        return ReturnJson.success("查询成功", tbOrderDtoList);
    }

    /**
     * 根据订单id查询订单
     *
     * @param orderId
     * @return
     */
    @AuthIgnoreApi
    @PostMapping("/getOrder")
    @ApiOperation(value = "根据订单id查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<TbOrderDto> getOrder(Integer orderId) {
        return orderService.selectOrderById(orderId);
    }

    /**
     * 创建订单
     *
     * @param shoppingCarIds
     * @return
     */
    @AuthIgnoreApi
    @ApiOperation(value = "创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shoppingCarIds", value = "购物车id数据集", dataType = "int[]", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/addOrder")
    public ReturnJson<TbOrderDataDto> addOrder(Integer[] shoppingCarIds) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
//		TbUser tbUser =new TbUser();
//		tbUser.setUserId(1111);
//		tbUser.setUserName("张三");
//		tbUser.setMobile("13540968022");
        return orderService.save(user, shoppingCarIds);
    }

    /**
     * 支付订单 根据订单id
     *
     * @param orderCode
     * @param messages
     * @param money
     * @param addressId
     * @return
     */
    @AuthIgnoreApi
    @PostMapping("/payOrderSuper")
    @ApiOperation(value = "支付订单 根据订单id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "订单编号", dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "messages", value = "留言数据 子订单的留言用逗号隔开", dataType = "String[]", paramType = "form"),
            @ApiImplicitParam(name = "money", value = "支付金额", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "addressId", value = "收货地址id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> payOrderSuper(String orderCode, String[] messages, Integer money, int addressId) {
        return orderService.payOrder(orderCode, messages, money, addressId);
    }

}
